package cn.iocoder.yudao.module.brokerage.merchant.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.brokerage.merchant.convert.BrokerageMerchantConvert;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.po.AgencyMerchantDO;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.vo.AppBrokerageMerchantInviteSumVO;
import cn.iocoder.yudao.module.brokerage.merchant.pojo.vo.AppBrokerageMerchantVO;
import cn.iocoder.yudao.module.brokerage.merchant.service.AppBrokerageMerchantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 推广商家")
@RestController
@RequestMapping("/brokerage/merchant")
@Validated
public class AppBrokerageMerchantController {
    @Resource
    private AppBrokerageMerchantService appBrokerageMerchantService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @GetMapping("/summary")
    @Operation(summary = "获得商家营业汇总")
    @PreAuthorize("@mm.isMerchant()")
    public CommonResult<AppBrokerageMerchantVO> getSummary() {
        Long shopId = shopInfoApi.getShopIdByMemberUserId(getLoginUserId());
        AgencyMerchantDO merchantDO = appBrokerageMerchantService.getOrCreate(shopId);
        return success(BrokerageMerchantConvert.INSTANCE.convert(merchantDO));
    }

    @GetMapping("/page")
    @Operation(summary = "获得推广商家列表")
    @Parameter(name = "pageNo", description = "页码", example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", example = "10")
    @PreAuthorize("@mm.isAgency()")
    public CommonResult<PageResult<AppBrokerageMerchantInviteSumVO>> getPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return success(appBrokerageMerchantService.getAgencyInviteSumPage(pageNo, pageSize, getLoginUserId()));
    }

}